package com.gph.saviorframework.fastdfs.monitor.service.impl;

import com.gph.saviorframework.fastdfs.GroupState;
import com.gph.saviorframework.fastdfs.StorageState;
import com.gph.saviorframework.fastdfs.monitor.model.*;
import com.gph.saviorframework.fastdfs.monitor.service.MonitorService;
import com.gph.saviorframework.fastdfs.monitor.util.JsshProxy;
import com.gph.saviorframework.fastdfs.monitor.util.Tools;
import com.gph.saviorframework.fastdfs.proto.ProtoCommon;
import com.gph.saviorframework.fastdfs.service.IStorageClientService;
import com.gph.saviorframework.fastdfs.service.ITrackerClientService;
import com.jcraft.jsch.JSchException;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MonitorServiceImpl implements MonitorService {

    @Autowired
    private ITrackerClientService trackerClientService;

    @Autowired
    private IStorageClientService storageClientService;

    public List<Group> listGroupInfo() throws IOException, JSchException {

        Date date = new Date();

        List<Group> groups = new ArrayList<Group>();
        GroupState[] states = trackerClientService.listGroups();

        Group group = null;
        Storage storage = null;
        for (GroupState state : states) {
            group = new Group();
            BeanUtils.copyProperties(state, group);
            StorageState[] storageStates = trackerClientService.listStorages(state.getGroupName());

            for (StorageState storageState : storageStates) {
                storage = new Storage();
                BeanUtils.copyProperties(storageState, storage);
                storage.setGroup(group);
                storage.setGroupName(group.getGroupName());
                storage.setCurStatus(ProtoCommon
                        .getStorageStatusCaption(storageState.getStatus()));
                storage.setCreated(date);
                group.getStorageList().add(storage);
            }
            groups.add(group);
        }

        /*String cmd = "ps -aux|grep fdfs";
        for (Machine machine : Tools.machines) {
            List<String> strList = new ArrayList<String>();
            if(machine.isConfigType()) {
                strList = Tools.exeRemoteConsole(machine.getIp(),
                        machine.getUsername(), machine.getPassword(), cmd);
            }
            else {
                strList = new JsshProxy(machine.getIp(), machine.getUsername(), machine.getPort(), machine.getSsh()).execute(cmd).getExecuteLines();
            }
            for (String str : strList) {
                if (str.contains("storage.conf")) {
                    for (Group temp : groups) {
                        temp.setCreated(date);
                        for (Storage storageTemp : group.getStorageList()) {
                            if (machine.getIp().equalsIgnoreCase(storageTemp.getIpAddr())) {
                                String[] strArrray = str.replaceAll(" +", ",")
                                        .split(",");
                                storageTemp.setCpu(strArrray[2]);
                                storageTemp.setMem(Float.parseFloat(strArrray[3]));
                            }
                        }
                    }
                }
            }
        }*/

        return groups;
    }

    public List<Group> listGroups() throws IOException {
        return null;
    }

    public List<Storage> listStorage(String groupName) throws IOException {
        return null;
    }

    public List<Storage> listStorageTop(String ipaddr) throws IOException {
        return null;
    }

    public List<Line> listStorageLines(String groupName) throws IOException {
        return null;
    }

    public List<Line> getNetTrafficLines(String ip, String start, String end) {
        return null;
    }

    public Line getListStoragesInfo(String ip, String startTime, String endTime) throws IOException {
        return null;
    }

    public StorageHour getStorageByIp(String ip) throws IOException {
        return null;
    }

    public List<Group> getAllGroups() throws IOException {
        return null;
    }

    public List<Line> getListFileCountStorage(String ip, String startTime, String endTime) throws IOException {
        return null;
    }

    public void saveFile(FdfsFile f) throws IOException {

    }

    public List<GroupDay> getGroupsByName(String groupName) throws IOException {
        return null;
    }
}
